package com.abc.cloud.controller;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

import com.abc.cloud.common.utils.LocalDateTimeUtil;
import com.abc.cloud.common.utils.UserHolder;
import com.abc.cloud.dto.comment.CommentUser;
import com.abc.cloud.entity.SensitiveWordEntity;
import com.abc.cloud.entity.UserEntity;
import com.abc.cloud.service.SensitiveWordService;
import com.abc.cloud.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.CommentEntity;
import com.abc.cloud.service.CommentService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;



/**
 * 评论表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-27 13:09:54
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = commentService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		CommentEntity comment = commentService.getById(id);
        return Result.success(comment, "comment");
    }

    /**
     * 根据文件id查出评论列表
     */
    @RequestMapping("/selectByFileId/{id}")
    public Result selectByFileId(@PathVariable Long id){
        List<CommentUser>  commentUsers =  commentService.getCommentByFileId(id);
        return Result.success(commentUsers,"");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody CommentEntity comment){
        Long id = UserHolder.getUser().getId();
        comment.setUserId(id);
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setStatus(1);
		commentService.save(comment);
        return Result.success(null, "");
    }


    /**
     * 添加回复评论
     */
    @RequestMapping("/addReply")
    public Result addReply(@RequestBody Map<String,Object> requestBody){
//        System.out.println(requestBody);
        Long pId = Long.valueOf(requestBody.get("pId").toString());
        String content = requestBody.get("content").toString();


        Long userId = UserHolder.getUser().getId();
        CommentEntity commentEntity = new CommentEntity();
        CommentEntity commentEntity1 = commentService.getById(pId);
        BeanUtils.copyProperties(commentEntity1,commentEntity,"id");
        // 拷贝之后，看 pid 是否为空，如为空，则说明父评论没有pid
        if(commentEntity.getPId()==null){
            commentEntity.setPpId(pId);
        }
        commentEntity.setUserId(userId);
        commentEntity.setPId(pId);
        commentEntity.setContent(content);
        commentEntity.setCreateTime(new Date());
        commentEntity.setUpdateTime(new Date());
        commentService.save(commentEntity);
        return Result.success(null,"");
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CommentEntity comment){
		commentService.updateById(comment);

        return Result.success(null, "");
    }

    /**
     * 删除
     */
    @RequestMapping("/admin/delete")
    public Result delete(@RequestBody Long[] ids){
        // 查出所有的评论
        List<CommentEntity> commentAll  = commentService.list();
        for(Long id:ids){
            for(CommentEntity c:commentAll){
                if(id.equals(c.getPId())){
                    return Result.fail(null,"存在子评论，不可删除！");
                }
            }
        }
		commentService.removeByIds(Arrays.asList(ids));
        return Result.success(null, "");
    }

    // 后台接口

    /**
     * 获取评论列表
     */
    @RequestMapping("/admin/list")
    public Result commentList(@RequestParam Map<String, Object> params){
//        PageUtil page = sensitiveWordService.queryPage(params);
//        return Result.success(page, "page");
        PageUtil pageUtil = commentService.getCommentList(params);
        return Result.success(pageUtil, "success");
    }

    /**
     * 修改评论状态
     */
    @RequestMapping("/admin/changestatus")
    public Result changeCommentStatus(@RequestBody CommentEntity commentEntity){
        commentEntity.setUpdateTime(new Date());
        commentService.updateById(commentEntity);
        return Result.success(null,"");
    }

}
